package com.sinaapp.gavinzhang.ConcurrentProgramming;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * 两个线程join进来后，会在两个线程执行完后再执行此线程
 * Created by gavin on 15-9-1.
 */
public class JoinTest {
    public static void main(String[] args)
    {
        DataSourcesLoader dataSourcesLoader = new DataSourcesLoader();
        NetWorkLoader netWorkLoader = new NetWorkLoader();
        Thread t1 = new Thread(dataSourcesLoader,"t1");
        Thread t2 = new Thread(netWorkLoader,"t2");
        t1.start();
        t2.start();

        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("finish");
    }
}


class DataSourcesLoader implements Runnable{
    public void run() {
        System.out.println("begin "+new Date());
        try {
            TimeUnit.SECONDS.sleep(4);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("end "+new Date());
    }
}

class NetWorkLoader implements Runnable{
    public void run() {
        System.out.println("begin "+new Date());
        try {
            TimeUnit.SECONDS.sleep(6);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("end "+new Date());
    }
}